package com.css.fxfzysx.modules.vaolEngineering.repository.impl;

import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxDevicesEntity;
import com.css.fxfzysx.modules.lifeline.vo.basic.DestructiveStateVo;
import com.css.fxfzysx.modules.vaolEngineering.entity.newEntity.CztSmxDevicesValueEntity;
import com.css.fxfzysx.modules.vaolEngineering.param.update.DevicesParam;
import com.css.fxfzysx.modules.vaolEngineering.repository.CeVulnerabilityRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class CeVulnerabilityRepositoryImpl implements CeVulnerabilityRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public boolean saveDevices(SmxDevicesEntity entity) {
        String sql = "insert into czt_smx_devices values (:id,:inObjectName,:location,:objectLon,:objectLat,:deviceType,:deviceName,:model,:inFloor,:fixedForm,:builtEra,:fortification,:boltDiameter,:purchaseCost,:manufacturers,:size,:weight,:aboveGroundHeight,:naturalPeriod,:site,:region,:totalHeight,:versionId,:tableId,:delFlag,:serialNumber,ST_GeometryFromText(:geom,4490),:createUser,:createTime,:updateUser,:updateTime,:extends1,:extends2,:extends3,:extends4,:extends5,:extends6,:extends7,:extends8,:extends9,:extends10,:installationSituation)";
        BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(entity);
        return namedParameterJdbcTemplate.update(sql,source)>0;
    }

    @Override
    public List<SmxDevicesEntity> getByVersionId(String id) {
        try {
            String sql = "select * from czt_smx_devices where version_id = '"+id+"'";
            return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(SmxDevicesEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void saveDevicesValue(CztSmxDevicesValueEntity entity) {
        jdbcTemplate.update("insert into czt_smx_devices_value  (id,devices_id,intensity,k1,k2,k3,ao,amax,result,fh,fv,fh_correct,fv_correct ) values (?,?,?,?,?,?,?,?,?,?,?,?,? )",
                entity.getId(),entity.getDevicesId(),entity.getIntensity(),entity.getK1(),entity.getK2(),entity.getK3(),entity.getAo(),entity.getAmax(),entity.getResult(),entity.getFh(),entity.getFv(),entity.getFhCorrect(),entity.getFvCorrect());
    }

    @Override
    public SmxDevicesEntity getByTableId(String id) {
        try {
            String sql = "select * from czt_smx_devices where table_id = '"+id+"'";
            return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(SmxDevicesEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public List<CztSmxDevicesValueEntity> getValueByDevicesId(String id) {
        try {
            String sql = "select * from czt_smx_devices_value where devices_id = '"+id+"'";
            return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(CztSmxDevicesValueEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public SmxDevicesEntity getById(String id) {
        try {
            String sql = "select * from czt_smx_devices_value where id = '"+id+"'";
            return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(SmxDevicesEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void updateDevicesById(DevicesParam entity) {
        jdbcTemplate.update("update czt_smx_devices set device_type=?,in_floor=?,weight=?,above_ground_height=?,natural_period=?,site=?,region=?,total_height=? where id=?",
                entity.getDeviceType(),entity.getInFloor(),entity.getWeight(),entity.getAboveGroundHeight(),entity.getNaturalPeriod(),entity.getSite(),entity.getTotalHeight(),entity.getId());
    }

    @Override
    public DestructiveStateVo getByBasicInfo(SmxDevicesEntity entity) {
        try {
            StringBuilder sql = new StringBuilder("select * from czt_smx_basic_library_devices where floor = '" + entity.getInFloor() + "' and installation_situation = '"+entity.getInstallationSituation()+"' and range = " + 0);
            return jdbcTemplate.queryForObject(sql.toString(), new BeanPropertyRowMapper<>(DestructiveStateVo.class));
        } catch (DataAccessException e) {
            return new DestructiveStateVo();
        }
    }
}
